package gui_layer;

import java.awt.Point;
import java.util.List;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import model_layer.Supplier;
import ctr_layer.SupplierCtr;

@SuppressWarnings("serial")
public class ManageSupplierGUI extends UniversalManageWindow {
	
	private SupplierCtr supplctr;
	private List<Supplier> suppliers;
	private String[] column_names = { "ID", "Name", "CVR", "Phone Number", "Email", "Address", "Zip Code", "City"};
	private Object[][] filling;
	
	public ManageSupplierGUI()
	{
		supplctr = new SupplierCtr();
		setTitle("Manage Suppliers");
		search_lbl.setText("Name/ID");
		show_deleted_chckbox.setText("Deleted Suppliers");
		setBounds(100, 100, 692, 431);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		this.addInternalFrameListener(new InternalFrameAdapter()
		{
			@Override
			public void internalFrameActivated(InternalFrameEvent arg0)
			{
				dynamically_search();
				
			}
		});
		
		show_non_deleted();
	}

	@Override
	protected void show_deleted() {
		suppliers = supplctr.get_deleted_suppliers();
		dynamically_search();
	}

	@Override
	protected void show_non_deleted() {
		suppliers = supplctr.get_non_deleted_suppliers();
		filling = new Object[suppliers.size()][8];
		int i = 0;
		for (Supplier temp : suppliers)
		{
			filling[i][0] = temp.getId();
			filling[i][1] = temp.getName();
			filling[i][2] = temp.getCvr();
			filling[i][3] = temp.getPhone_nr();
			filling[i][4] = temp.getEmail();
			filling[i][5] = temp.getAddress();
			filling[i][6] = temp.getZipcode();
			filling[i][7] = temp.getCity();
			i++;
		}
		prepare_table (filling, column_names);

	}

	protected void create_new()
	{
		NewSupplierGUI instance = new NewSupplierGUI(null, false);
		this.getDesktopPane().add(instance);
		instance.setVisible(true);
		Point window_corner = this.getLocation();
		instance.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
		instance.addInternalFrameListener(new InternalFrameAdapter()
		{
			public void internalFrameClosing(InternalFrameEvent e)
			{
				dynamically_search();
			}
		});

	}

	protected void edit()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			String id = String.valueOf(table.getModel().getValueAt(row, 0));
			NewSupplierGUI nc_gui = new NewSupplierGUI(supplctr.find_supplier_by_id(id), true);
			this.getDesktopPane().add(nc_gui);
			Point window_corner = this.getLocation();
			nc_gui.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
			nc_gui.setVisible(true);
			nc_gui.addInternalFrameListener(new InternalFrameAdapter()
			{
				@Override
				public void internalFrameClosing(InternalFrameEvent arg0)
				{
					dynamically_search();
				}
			});
		}
	}

	@Override
	protected void view() {
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			String id = String.valueOf(table.getModel().getValueAt(row, 0));
			NewSupplierGUI gui = new NewSupplierGUI(supplctr.find_supplier_by_id(id), false);
			getDesktopPane().add(gui);
			Point window_corner = this.getLocation();
			gui.setLocation((int) window_corner.getX() + 20, (int) window_corner.getY() + 20);
			gui.setVisible(true);
		}

	}

	@Override
	protected void delete()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			String id = String.valueOf(table.getModel().getValueAt(row, 0));
			if(JOptionPane.showConfirmDialog(this, "Are you sure you wish to delete this Supplier?",
					"Delete Supplier", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				if(supplctr.delete_supplier(id))
				{
					JOptionPane.showMessageDialog(this, "Successfully deleted supplier!", "Success", JOptionPane.PLAIN_MESSAGE);
					show_non_deleted();
				}
				else JOptionPane.showMessageDialog(this, "Error!\nSupplier was not deleted!", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}

	}

	@Override
	protected void restore() {
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			String id = String.valueOf(table.getModel().getValueAt(row, 0));
			if(JOptionPane.showConfirmDialog(this, "Are you sure you wish to restore this Supplier?",
					"Restore Supplier", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				if(supplctr.restore_supplier(id))
				{
					JOptionPane.showMessageDialog(this, "Successfully restored supplier!", "Success", JOptionPane.PLAIN_MESSAGE);
					show_non_deleted();
				}
				else JOptionPane.showMessageDialog(this, "Error!\nSupplier was not restored!", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}

	}

	@Override
	protected void dynamically_search() {
		String text = search_field.getText();
		MyTableModel model = (MyTableModel) table.getModel();
		Vector<Object[]> vector = new Vector<Object[]>();
		Object[] data;
		for(Supplier suppl : suppliers)
		{
			if(suppl.getName().startsWith(text) || suppl.getId().startsWith(text))
			{
				
				data = new Object[8];
				data[0] = suppl.getId();
				data[1] = suppl.getName();
				data[2] = suppl.getCvr();
				data[3] = suppl.getPhone_nr();
				data[4] = suppl.getEmail();
				data[5] = suppl.getAddress();
				data[6] = suppl.getZipcode();
				data[7] = suppl.getCity();
				vector.add(data);
			}
		}
		Object[][] filling = new Object[vector.size()][8];
		for(int i = 0; i < vector.size(); i++)
		{
			filling[i] = vector.get(i);
		}
		model.setDataVector(filling, column_names);
		
	}

}
